<?php

namespace App\Services;

class ErrorCodeService
{
    const
        /**
         * 系统级通用错误码
         */
        SUCCESS = 10000,
        ERROR_DEFAULT = 50000,
        ERROR_PARAM_IS_EMPTY = 10001,
        ERROR_PARAM_NOT_EXIST = 10002,
        ERROR_USER_NOT_LOGIN = 10003;


    public static function errorArr(): array
    {
        return [
            self::SUCCESS => 'success',
            self::ERROR_DEFAULT => "%s",

            //参数
            self::ERROR_PARAM_IS_EMPTY => "参数%s不能为空!",
            self::ERROR_PARAM_NOT_EXIST => "缺少参数%s",
            self::ERROR_USER_NOT_LOGIN => "登陆状态已失效,请重新登陆",

        ];
    }

    /**
     * 根据错误码 获取错误信息
     * @param $code
     * @param string[] $param
     * @return string
     * User Niu Yong Qiang
     * Date 2021/7/23 18:58
     */
    public static function getErrorMessage($code, array $param = ['']): string
    {
        $errorArr = self::errorArr();
        $msg = $errorArr[$code] ?? $errorArr[self::ERROR_DEFAULT];
        $param = is_array($param) ? $param : [$param];

        return !empty($param) ? vsprintf($msg, $param) : $msg;
    }
}
